/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.explorer;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.measure.unit.Unit;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import se.kth.lwr.jeat.core.Activity;
import se.kth.lwr.jeat.core.ImpactModel;
import se.kth.lwr.jeat.core.Report;
import se.kth.lwr.jeat.core.UnitHandler;

/**
 * A class that creates a GUI which visualize selected models. In general you
 * just need one instance of this class. Setup the Explorer by attaching
 * one or several ESA models to it (<code>attachModel(Activity model)</code>),  
 * select ImpactModels of interest (<code>useImpactModel(ImpactModel model)</code>)
 * and finally visualize the results of the model(s) by calling <code>visualize()</code>.
 * Control of units displayed is possible trough the {@link Report} class. 
 * <code> Report.getUnitHandler() <code>.
 * 
 * Jeat Explorer skapades för att hålla GUI koden så ren som möjligt.
 *
 * @author davidev
 */
public class JeatExplorer {
    // referens to GUI
    JeatGUI gui;
    
    //reference to selected activities/LCA models that should be inspected
    ArrayList<Activity> activities=new ArrayList<>();
    
    
    //a reference to all ImpactModels that should be used
    private HashSet<ImpactModel> impactModels = new HashSet<>();
    
    
    
    
/**Creates a new explorer (each explorer will have its own GUI). 
 * 
 * 
 * @throws InterruptedException
 * @throws InvocationTargetException 
 */
    public JeatExplorer() throws InterruptedException, InvocationTargetException {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JeatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JeatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JeatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JeatGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        /*
         * Create the gui and ensure that it is initiated in the event 
         * dispatching thread.
         */
        if(!SwingUtilities.isEventDispatchThread()){
            
        java.awt.EventQueue.invokeAndWait(new Runnable() {

                @Override
            public void run() {
                gui=new JeatGUI(JeatExplorer.this);
            }
        });
        
    }else{
            gui=new JeatGUI(JeatExplorer.this);
        }
    }
    
    /**
     * Adds models (activities) for the explorer to visualize and setup model 
     * results in the components of the GUI
     * 
     * @param activity The model for wich results should be visualized
     */
    public void attachModel(Activity activity){
        activities.add(activity);
        setup();
        
    }
    
    /**
     * Selects ImpactModels to visualize. Use this method repeatedly to select
     * several impactModels;
     */
    
    public void useImpactModel(ImpactModel model){
        impactModels.add(model);
        setup();
        
    }
    
    /**
     * Delegates to the GUI to setup the results of the models (activities) 
     * attached to this Explorer in visual components
     * 
     */
    
    private void setup(){
        gui.setup(activities,impactModels);
    }
    
    /**
     * Shows the GUI on screen
     */
    
    public void visualize(){
        gui.setVisible(true);
    }

    public Set<ImpactModel> getImpactModels() {
        return impactModels;
    }



    public List<Activity> getActivities() {
        return activities;
    }
    
    
    
    
    
    
    
}
